package com.gitee.aa76111.tdd.args;

import com.gitee.aa76111.tdd.args.enumeration.ArgTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;


@Slf4j
public class Schema {
    private static final String ARG_SPEC_DIVIDE = ",";

    private List<ArgSpec> argSpecs;

    public Schema(String schemaLine) {
        argSpecs = Arrays.stream(schemaLine.trim().split(ARG_SPEC_DIVIDE))
                .map(ArgSpec::new)
                .collect(toList());
    }

    public Object getValue(String key, String strValue) {
        return argSpecs.stream()
                .filter(argSpec -> argSpec.getLabel().equals(key))
                .findFirst()
                .map(argSpec -> ArgTypeEnum.valueOf(argSpec.getType().toUpperCase()).value(strValue))
                .orElseThrow(() -> new IllegalArgumentException("没有此参数: " + key));
    }
}
